﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Communergy.Common;

namespace Communergy.Data
{
    public class DataAccessLayer// : IRealtimeRepository, IHistoricRepository, IMeterRepository
    {

        IUnitOfWork unitOfWork;
        IRepositoryFactory repositoryFactory;

        public DataAccessLayer(IRepositoryFactory repositoryFactory)
        {            
            this.repositoryFactory = repositoryFactory;
            unitOfWork = this.repositoryFactory.CreateUnitOfWork();
        }

        #region IRealtimeRepository
        public void Insert_ElectricityRealtime(string meterId, List<RealtimeEnergyReading> realtimeEnergyReadings)
        {
            var repository = repositoryFactory.CreateRealtimeRepository(unitOfWork);
            repository.Insert_ElectricityRealtime(meterId, realtimeEnergyReadings);
            unitOfWork.Save();
        }

        public List<RealtimeEnergyReading> Get_ElectricityRealtime(string meterId, DateTime dateFrom, DateTime dateTo)
        {
            var repository = repositoryFactory.CreateRealtimeRepository(unitOfWork);
            var data = repository.Get_ElectricityRealtime(meterId, dateFrom, dateTo);
            return data;
        }

        public RealtimeEnergyReading GetLastElectricityRealtime(string meterId)
        {
            var repository = repositoryFactory.CreateRealtimeRepository(unitOfWork);
            var data = repository.GetLastElectricityRealtime(meterId);
            return data;
        }

        public HistoricalEnergyReading GetCumulativeElectricityRealtime(string meterId, DateTime dateFrom, DateTime dateTo)
        {
            var repository = repositoryFactory.CreateRealtimeRepository(unitOfWork);
            var data = repository.GetCumulativeElectricityRealtime(meterId, dateFrom, dateTo);
            return data;
        }

        #endregion

        #region IHistoricRepository

        public List<HistoricalEnergyReading> GetHourly(string meterId, DateTime dateFrom)
        {
            var repository = repositoryFactory.CreateHistoricRepository(unitOfWork);
            var data = repository.GetHourly(meterId, dateFrom);
            return data;
        }


 
        public void Insert_ElectricityHourly(string meterId, List<HistoricalEnergyReading> hourlyEnergyReadings)
        {
            var repository = repositoryFactory.CreateHistoricRepository(unitOfWork);
            repository.Insert_ElectricityHourly(meterId, hourlyEnergyReadings);
            unitOfWork.Save();
        }

        public void Insert_ElectricityDaily(string meterId, List<HistoricalEnergyReading> dailyEnergyReadings)
        {
            var repository = repositoryFactory.CreateHistoricRepository(unitOfWork);
            repository.Insert_ElectricityDaily(meterId, dailyEnergyReadings);
            unitOfWork.Save();
        }

        public void Insert_ElectricityMonthly(string meterId, List<HistoricalEnergyReading> monthlyEnergyReadings)
        {
            var repository = repositoryFactory.CreateHistoricRepository(unitOfWork);
            repository.Insert_ElectricityMonthly(meterId, monthlyEnergyReadings);
            unitOfWork.Save();
        }


        #endregion



        #region IMeterRepository Members

        public Meter CreateNewMeter(Meter meterToCreate)
        {
            var repository = repositoryFactory.CreateMeterRepository(unitOfWork);
            var data = repository.CreateNewMeter(meterToCreate);
            return data;
        }

        public Meter RetrieveMeterDetails(string meterId)
        {
            var repository = repositoryFactory.CreateMeterRepository(unitOfWork);
            var data = repository.RetrieveMeterDetails(meterId);
            return data;
        }

        public void SaveMeterDetails(Meter meterToSave)
        {
            var repository = repositoryFactory.CreateMeterRepository(unitOfWork);
            repository.SaveMeterDetails(meterToSave);
        }

        public List<Meter> GetMetersForUser(string userId)
        {
            var repository = repositoryFactory.CreateMeterRepository(unitOfWork);
            var data = repository.GetMetersForUser(userId);
            return data;
        }

        #endregion



        public void InsertEnergyData(string localMeterId, List<EnergyData> newCurrentCostRealtimeReadings)
        {
            throw new NotImplementedException();
        }
    }
}
